package com.mago.tis.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.mago.tis.bean.LoginBean;
import com.mago.tis.service.ILoginService;
import com.mago.tis.serviceImpl.LoginService;

/**
 * Controller responsible for the Authentication of the user
 * 
 * @author Piyush
 * 
 */
public class LoginController extends BaseController {

	private static final Logger logger = Logger
			.getLogger(LoginController.class);

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.info("Login controller called");
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");

		// Set the bean
		LoginBean loginBean = new LoginBean();
		loginBean.setName(userName);
		loginBean.setPassword(password);

		// call the Service Layer
		ILoginService loginService = new LoginService();
		// Get the response from service layer
		boolean authenticated = loginService.login(loginBean);
		if (authenticated == false) {
			//request.setAttribute("message",
					//ExceptionMessage.USER_LOGIN_PASSWORD_INVALID);
			response.sendRedirect("pages/jsp/login.jsp");
		} else {
			rd = request.getRequestDispatcher("/pages/jsp/welcome.jsp");
			rd.forward(request, response);
		}
		logger.info("Login controller call ended : Response being returned");
	}
}
